package com.izhaowo.recommend.contrl;

import com.izhaowo.recommend.util.HBaseUtil;
import com.izhaowo.recommend.vo.*;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by HASEE on 2019/7/19.
 */
public class RecommendWeddingPlanner {

    private static final Logger logger = LoggerFactory.getLogger(RecommendWeddingPlanner.class);

    /**
     * weddingId 查询用户婚礼信息 主要获取user_id ，wedding_date
     *
     * @param weddingId
     * @return
     */
    public static UserWeddingVO getUserWeddingById(String weddingId) {
        Result r = HBaseUtil.getRow("v2_rp_tb_user_wedding", Bytes.toBytes(weddingId));
        if (null != r && !r.isEmpty()) {
            UserWeddingVO vo = Result2Vo.getUserWeddingVO(r);
            r = HBaseUtil.getRow("v2_rp_tb_user_wedding_location", Bytes.toBytes(weddingId));
            if (null != r && !r.isEmpty()) {
                Result2Vo.fillUserWeddingVO(r, vo);
                return vo;
            }
        }
        return null;
    }

    /**
     * 根据婚礼id查询用户标签
     *
     * @param weddingId
     * @return
     */
    public static WeddingUserTagVO queryWeddingUserTag(String weddingId) {
        Result r = HBaseUtil.getRow("v2_rp_tb_wedding_user_tag", Bytes.toBytes(weddingId));
        if (null != r && !r.isEmpty()) {
            return Result2Vo.getWeddingUserTagVO(r);
        }
        return null;
    }

    /**
     * 根据酒店id查询酒店信息
     *
     * @param hotelId
     * @return
     */
    public static HotelVO queryHotelVOById(String hotelId) {
        Result r = HBaseUtil.getRow("v2_rp_tb_hotel", Bytes.toBytes(hotelId));
        if (null != r && !r.isEmpty()) {
            return Result2Vo.getHotelVO(r);
        }
        return null;
    }

    /**
     * 根据user_id 查询crm  用户信息查看是否策划师推荐,返回second_channel_id
     *
     * @param user_id
     * @return
     */
    public static String getWorkerIdByUserId(String user_id) {
        Result r = HBaseUtil.getRow("v2_rp_tb_user_channel", Bytes.toBytes(user_id));
        String second_channel_id = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("second_channel_id")));
        if (null == second_channel_id || "null".equals(second_channel_id)) {
            second_channel_id = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("sub_channel_id")));
        }
        return second_channel_id;
    }

    public static String getWorkerIdByUserIdV2(String user_id) {
        Result r = HBaseUtil.getRow("v2_rp_worker_bonus", Bytes.toBytes(user_id));
        String second_channel_id = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("worker_id")));

        return second_channel_id;
    }

    public static String getWorkerIdByUserIdV3(String channel_id) {
        Result r = HBaseUtil.getRow("v2_rp_channel", Bytes.toBytes(channel_id));
        String second_channel_id = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("source_id")));

        return second_channel_id;
    }

    /**
     * 根据worker_id 查询职业人信息
     *
     * @param workerId
     * @return
     */
    public static WorkerVO getWorkerVOById(String workerId) {
        Result r = HBaseUtil.getRow("v2_rp_tb_worker", Bytes.toBytes(workerId));
        if (null != r && !r.isEmpty()) {
            WorkerVO vo = Result2Vo.getWorkerVO(r);
            getWorkerLocation(vo);
            return vo;
        }
        return null;
    }


    public static Map<String, String> getProvinceMapById(String id) {
        Result r = HBaseUtil.getRow("tb_province", Bytes.toBytes(id));
        if (null != r && !r.isEmpty()) {
            return Result2Vo.getProvinceMap(r);
        }
        return null;
    }

    public static UserByCrmVO getUserByMsisdn(String msisdn) {
        Result r = HBaseUtil.getRow("v2_rp_tb_user4crm", Bytes.toBytes(msisdn));
        if (null != r && !r.isEmpty()) {
            return Result2Vo.getUserByCrmVO(r);
        }
        return null;
    }

    public static UserByCrmVO getUserByMsisdnV2(String msisdn) {
        Result r = HBaseUtil.getRow("v2_rp_customer4crm", Bytes.toBytes(msisdn));
        if (null != r && !r.isEmpty()) {
            return Result2Vo.getUserByCrmVOV2(r);
        }
        return null;
    }

    /**
     * 通过user_Id获取回单信息
     *
     * @param userId
     * @return
     */
    public static UserReserveVO getUserReserveByUserId(String userId) {
        Result r = HBaseUtil.getRow("v2_rp_tb_user_reserve", Bytes.toBytes(userId));
        if (r != null && !r.isEmpty()) {
            return Result2Vo.getUserReserveVO(r);
        }
        return null;
    }

    /**
     * 通过wedding_id获取推荐结果，进行验证用。
     *
     * @param crmUserIds
     * @return
     */
    public static List<PlannerRecomRecordVO> queryPlannerRecomRecordByCrmUserIds(List<String> crmUserIds) {
        List<PlannerRecomRecordVO> result = new ArrayList<>();
        for (String uid : crmUserIds) {
            // Result r = HBaseUtil.getRow("rp_tb_planner_recom_record", Bytes.toBytes(uid));
//            ResultScanner scanner = HBaseUtil.queryRowsWithKeys("rp_tb_planner_recom_record", crmUserIds + "=0", crmUserIds + "=zzzzz");
            ResultScanner scanner = HBaseUtil.queryRowsWithKeys("v2_rp_tb_planner_recom_record", uid + "=", uid + ">");
            for (Result r : scanner) {
                if (null != r && !r.isEmpty()) {
                    result.add(Result2Vo.getPlannerRecomRecordVO(r));
                }
            }
        }
        return result;
    }

    /**
     * ======================================================================================
     * 查询所有的职业人信息，并且以职业人名称为key ，职业人列表返回
     * ======================================================================================
     *
     * @return
     */
    public static Map<String, List<WorkerVO>> getNameWorkers(Filter filter) {

        ResultScanner rs = HBaseUtil.get("v2_rp_tb_worker", filter);
        Iterator<Result> it = rs.iterator();
        Map<String, List<WorkerVO>> result = new HashMap<>();
        while (it.hasNext()) {
            WorkerVO vo = Result2Vo.getWorkerVO(it.next());
            String name = vo.getName();
            List<WorkerVO> list = null;
            if (result.containsKey(name) && null != result.get(name)) {
                list = result.get(name);
            } else {
                list = new ArrayList<>();
            }
            list.add(vo);
            result.put(name, list);
        }
        return result;
    }


    public static List<PhotoPlannerVO> getPhotoPlanners(Filter filter) {

        ResultScanner rs = HBaseUtil.get("planner_photo_prescore", filter);
        Iterator<Result> it = rs.iterator();
        List<PhotoPlannerVO> list = new ArrayList<>(20);
        while (it.hasNext()) {
            PhotoPlannerVO vo = Result2Vo.getPhotoPlannerVO(it.next());
            list.add(vo);
        }
        return list;
    }

    public static List<WeddingScenePhotoVO> getWeddingScenePhotoVO(String start, String end) {

        ResultScanner rs = HBaseUtil.queryRowsWithKeys("planner_photo_prescore_result", start, end);
        Iterator<Result> it = rs.iterator();
        List<WeddingScenePhotoVO> list = new ArrayList<>(20);
        while (it.hasNext()) {
            WeddingScenePhotoVO vo = Result2Vo.getWeddingScenePhotoVO(it.next());
            list.add(vo);
        }
        return list;
    }


    /**
     * 通过过滤条件查询职业人信息
     *
     * @param filter
     * @return
     */
    public static List<WorkerVO> getAllWorkers(Filter filter) {
//        Filter filter = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("status"),
//                CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(status)));
        ResultScanner rs = HBaseUtil.get("v2_rp_tb_worker", filter);
        Iterator<Result> it = rs.iterator();
        List<WorkerVO> result = new ArrayList<>();
        while (it.hasNext()) {
            result.add(Result2Vo.getWorkerVO(it.next()));
        }
        return result;
    }

    /**
     * 通过过滤条件查询谈单职业人信息
     *
     * @param filter
     * @return
     */
    public static List<TalkOrderPlannerVO> getTalkOrderPlanners(Filter filter) {
        ResultScanner rs = HBaseUtil.get("tb_talk_order_planner_info", filter);
        Iterator<Result> it = rs.iterator();
        List<TalkOrderPlannerVO> result = new ArrayList<>();
        while (it.hasNext()) {
            result.add(Result2Vo.getTalkOrderPlannerVO(it.next()));
        }
        return result;
    }

    public static List<YingjingPlannerSectionVO> getYingJingPlannerSection(Filter filter) {
        ResultScanner rs = HBaseUtil.get("tb_yingjing_planner_section", filter);
        Iterator<Result> it = rs.iterator();
        List<YingjingPlannerSectionVO> result = new ArrayList<>();
        while (it.hasNext()) {
            result.add(Result2Vo.getYingJingPlannerSectionVO(it.next()));
        }
        return result;
    }

    public static List<YingjingPlannerPersonalityVO> getYingJingPlannerPersonality(Filter filter) {
        ResultScanner rs = HBaseUtil.get("tb_yingjing_planner_personality", filter);
        Iterator<Result> it = rs.iterator();
        List<YingjingPlannerPersonalityVO> result = new ArrayList<>();
        while (it.hasNext()) {
            result.add(Result2Vo.getYingjingPlannerPersonalityVO(it.next()));
        }
        return result;
    }


    /**
     * ======================================================================================
     * 查询职业人推荐排名
     * ======================================================================================
     *
     * @return
     */
    public static List<PlannerRecomRankVO> queryPlannerRecomRank() {
        ResultScanner rs = HBaseUtil.get("rp_tb_low_and_high_budget", null);
        Iterator<Result> it = rs.iterator();
        List<PlannerRecomRankVO> result = new ArrayList<>();
        PlannerRecomRankVO first = null;
        while (it.hasNext()) {
            PlannerRecomRankVO vo = Result2Vo.getPlannerRecomRankVO(it.next());
            if (vo.isFrist()) {
                first = vo;
            } else {
                fixPlannerRecomRankVO(first, vo);
                result.add(vo);
            }

        }
        return result;
    }


    public static List<PlannerRecomRankVO> queryPlannerRecomRankVersion2() {
        ResultScanner rs = HBaseUtil.get("v2_rp_tb_planner_low_budget_rank", null);
        Iterator<Result> it = rs.iterator();
        List<PlannerRecomRankVO> result = new ArrayList<>();
        Map<String, List<String>> workerTags = queryPlannerWeddingTag();
        while (it.hasNext()) {
            PlannerRecomRankVO vo = Result2Vo.getPlannerRecomRankVOVersion2(it.next());
            if (workerTags.containsKey(vo.getWorkerId())) {
                vo.setTags(workerTags.get(vo.getWorkerId()));
            }
            result.add(vo);
        }
        return result;
    }


    public static List<PlannerRecomRankVO> queryPlannerRecomRankJJ(Integer inArea) {
//        ResultScanner rs = HBaseUtil.get("v2_jj_xian_planner", null);
        StringBuilder sb = new StringBuilder();
        sb.append(inArea);
        String baseRowKey = sb.reverse().toString();
        ResultScanner rs = HBaseUtil.queryRowsWithKeys("v3_jj_xian_planner", baseRowKey + "=", baseRowKey + ">");
        Iterator<Result> it = rs.iterator();
        List<PlannerRecomRankVO> result = new ArrayList<>();
        Map<String, List<String>> workerTags = queryPlannerWeddingTag();
        while (it.hasNext()) {
            PlannerRecomRankVO vo = Result2Vo.getPlannerRecomRankVOJJ(it.next());
            if (workerTags.containsKey(vo.getWorkerId())) {
                vo.setTags(workerTags.get(vo.getWorkerId()));
            }
            result.add(vo);
        }
        return result;
    }

    public static Map<String, List<String>> queryPlannerWeddingTag() {
        ResultScanner rs = HBaseUtil.get("v2_rp_tb_planner_wedding_tag", null);
        Iterator<Result> it = rs.iterator();
        Map<String, List<String>> result = new HashMap<>();
        while (it.hasNext()) {
            result.putAll(Result2Vo.getPlannerWeddingTag(it.next()));
        }
        return result;
    }

    public static List<RecommandResultVO> queryRecomendResultMsg(String startKey, String endKey) {
        ResultScanner rs = HBaseUtil.queryRowsWithKeys("recom_planner_result_v3", startKey, endKey);
        Iterator<Result> it = rs.iterator();
        List<RecommandResultVO> list = new ArrayList<>();
        while (it.hasNext()) {
            RecommandResultVO vo = Result2Vo.getRecommandResultVO(it.next());
            if (StringUtils.isNotBlank(vo.getHotelId())) {
                String day = getQueryTime(vo.getStartTime());
                String start = vo.getHotelId() + "=" + day;
                String end = vo.getHotelId() + "=" + day + ">";
                List<HotelInAreaMsgVO> hotelMsg = queryHotelInAreaMsgDetail(start, end);
                if (!hotelMsg.isEmpty()) {
                    vo.setHotelIdInArea(hotelMsg.get(0).getInArea());
                    vo.setHotel_truth_workers(hotelMsg.get(0).getTruth_worker_count());
                } else {
                    logger.debug("______  have not hotel inarea msg ,hotelId is " + vo.getHotelId() + " ,hotel name is " + vo.getHotel());
                }

            }
            list.add(vo);
        }
        return list;
    }

    public static String getQueryTime(String startTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        try {
            Date date = sdf.parse(startTime);
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(date.getTime() - 6 * 60 * 60 * 1000L);
            return new SimpleDateFormat("yyyyMMdd").format(cal.getTime());
        } catch (Exception e) {
            logger.debug("_____  " + e.getMessage());
        }

        return "";
    }

    public static List<HotelInAreaMsgVO> queryHotelInAreaMsgDetail(String startKey, String endKey) {
        ResultScanner rs = HBaseUtil.queryRowsWithKeys("tb_hotel_inarea_msg_v3", startKey, endKey);
        Iterator<Result> it = rs.iterator();
        List<HotelInAreaMsgVO> list = new ArrayList<>();
        while (it.hasNext()) {
            HotelInAreaMsgVO vo = Result2Vo.getHotelInAreaMsgVO(it.next());
            list.add(vo);
        }
        return list;
    }


    public static Map<String, RecommandResultVO> queryRecomendResultMsgCount(String startKey, String endKey, String filter) {
        List<RecommandResultVO> list = queryRecomendResultMsg(startKey, endKey);
        Map<String, RecommandResultVO> res = new HashMap<>();
        RecommandResultVO temp;
        for (RecommandResultVO vo : list) {
            if (res.containsKey(vo.getWedding_id())) {
                temp = res.get(vo.getWedding_id());
            } else {
                temp = new RecommandResultVO();
                BeanUtils.copyProperties(vo, temp);
                Map<String, String> t = new HashMap<>();
                List<String> l = new ArrayList<>();
                Map<String, String> times = new HashMap<>();
                temp.setHasQuery(l);
                temp.setHasResult(t);
                temp.setQueryTimes(times);
            }

            List<String> l = temp.getHasQuery();
            l.add(vo.getInArea());
            temp.setHasQuery(l);

            Map<String, String> times = temp.getQueryTimes();
            times.put(vo.getInArea(), vo.getStartTime());
            temp.setQueryTimes(times);

            if (temp.getRecom().length() > 3) {
                Map<String, String> t = temp.getHasResult();
                t.put(temp.getInArea(), temp.getRecom());
                temp.setHasResult(t);
            }

            res.put(vo.getWedding_id(), temp);
        }

        if ("yes".equals(filter)) {
            Map<String, RecommandResultVO> tempMap = new HashMap<>();
            for (Iterator ite = res.entrySet().iterator(); ite.hasNext(); ) {
                Map.Entry entry = (Map.Entry) ite.next();
                RecommandResultVO v = (RecommandResultVO) entry.getValue();
                if (v.getHasResult().isEmpty()) {
                    tempMap.put(v.getWedding_id(), v);
                }
            }
            return tempMap;
        }

        return res;
    }


    public static List<HotelInAreaMsgVO> queryHotelInAreaMsg(String startKey, String endKey) {
        List<HotelInAreaMsgVO> list = queryHotelInAreaMsgDetail(startKey, endKey);
        Map<String, HotelInAreaMsgVO> res = new HashMap<>();
        HotelInAreaMsgVO temp;
        for (HotelInAreaMsgVO vo : list) {
            if (res.containsKey(vo.getHotel_id())) {
                temp = res.get(vo.getHotel_id());
            } else {
                temp = new HotelInAreaMsgVO();
                BeanUtils.copyProperties(vo, temp);
                Map<String, String> inAreaDay = new HashMap<>();
                temp.setInAreaDay(inAreaDay);
            }


            Map<String, String> inAreaDay = temp.getInAreaDay();
            inAreaDay.put(vo.getInArea(), vo.getDayStr());
            temp.setInAreaDay(inAreaDay);
            res.put(vo.getHotel_id(), temp);
        }

        HotelInAreaMsgVO[] vos = new HotelInAreaMsgVO[res.values().size()];
        return Arrays.asList(res.values().toArray(vos));
    }


    public static List<JJPlannerVO> querJJPlanner() {
        ResultScanner rs = HBaseUtil.get("v3_jj_xian_planner", null);
        Iterator<Result> it = rs.iterator();
        List<JJPlannerVO> result = new ArrayList<>();
        while (it.hasNext()) {
            result.add(Result2Vo.getJJPlanner(it.next()));
        }
        return result;
    }


    public static List<PlannerRecomRankVO> queryHighPlannerRecomRank() {
        ResultScanner rs = HBaseUtil.get("v2_rp_tb_high_budget_rank", null);
        Iterator<Result> it = rs.iterator();
        List<PlannerRecomRankVO> result = new ArrayList<>();
        PlannerRecomRankVO first = null;
        Map<String, List<String>> workerTags = queryPlannerWeddingTag();

        while (it.hasNext()) {
            PlannerRecomRankVO vo = Result2Vo.getHighPlannerRecomRankVO(it.next());
            if (workerTags.containsKey(vo.getWorkerId())) {
                vo.setTags(workerTags.get(vo.getWorkerId()));
            }
            if (vo.isFrist()) {
                first = vo;
            } else {
                fixPlannerRecomRankVO(first, vo);
                result.add(vo);
            }

        }
        return result;
    }

    public static void fixPlannerRecomRankVO(PlannerRecomRankVO first, PlannerRecomRankVO vo) {
        if (null == first || !first.getWorkerId().equals(vo.getWorkerId())) {
            return;
        }
        vo.setScoreFlag6(first.getScoreFlag6());
        vo.setScoreFlag7(first.getScoreFlag7());
        vo.setScoreFlag8(first.getScoreFlag8());
        vo.setScoreFlag9(first.getScoreFlag9());
        vo.setScoreFlag10(first.getScoreFlag10());
        vo.setScoreFlag11(first.getScoreFlag11());
        vo.setScoreFlag12(first.getScoreFlag12());
        vo.setScoreFlag13(first.getScoreFlag13());
    }

    /**
     * 获取所有类别职业信息
     *
     * @return
     */
    public static List<VocationVO> getVocations() {
        ResultScanner rs = HBaseUtil.get("v2_rp_tb_vocation", null);
        Iterator<Result> it = rs.iterator();
        List<VocationVO> result = new ArrayList<>();
        while (it.hasNext()) {
            Result r = it.next();
            VocationVO vo = Result2Vo.getVocationVO(r);
            result.add(vo);
        }
        return result;
    }

    public static boolean getSmallMarketMsg(String hotelIdSection) {
        Result r = HBaseUtil.getRow("mv_tb_small_market_info", Bytes.toBytes(hotelIdSection));
        if (null != r && !r.isEmpty()) {
            logger.info("___  check small market has " + hotelIdSection);
            return true;
        }

        logger.info("___  check small market has no " + hotelIdSection);
        return false;
    }

    /**
     * 以一些职业人的信息查询职业人当前情况
     * <p>
     * //     * @param listVo
     *
     * @param startYearMonth
     * @return
     */
    public static Map<String, List<WorkerScheduleVO>> getWorkerScheduleDates(List<String> ids, String startYearMonth, String endYearMonth) {
        Map<String, List<WorkerScheduleVO>> result = new HashMap<>();
        for (String id : ids) {
            ResultScanner rs = HBaseUtil.queryRowsWithKeys("v2_rp_tb_worker_schedule", id + "=2=" + startYearMonth, id + "=2=" + endYearMonth);
            Iterator<Result> it = rs.iterator();
            List<WorkerScheduleVO> list = new ArrayList<>();
            while (it.hasNext()) {
                WorkerScheduleVO vo = Result2Vo.getWorkerScheduleVO(it.next());
                if ("1".equals(vo.getIsUse()) && !"1".equals(vo.getFlag()))
                    list.add(vo);
            }
            result.put(id, list);
        }
        return result;
    }

    /**
     * 根据ids查询用户信息
     *
     * @param ids
     * @return
     */
    public static Map<String, UserVO> getUserVO(List<String> ids) {
        Map<String, UserVO> result = new HashMap<>();
        for (String id : ids) {
            if (null != id && !"".equals(id)) {
                Result r = HBaseUtil.getRow("v2_rp_tb_user4user", Bytes.toBytes(id));
                if (null != r && !r.isEmpty()) {
                    result.put(id, Result2Vo.getUserVO(r));
                }
            }
        }
        return result;
    }

    public static Map<String, UserVO> getUserVOList2Map(List<String> ids) {
        Map<String, UserVO> result = new HashMap<>(400);
        int size = ids.size();
        List<List> list = new ArrayList<>();
        List<String> temp = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            temp.add(ids.get(i));
            if (i % 100 == 0 || i == size - 1) {
                list.add(temp);
                if (i != size - 1) temp = new ArrayList<>();
            }
        }

        for (List l : list) {
            Result[] rs = HBaseUtil.getRows("v2_rp_tb_user4user", l);
            for (Result r : rs) {
                if (null != r && !r.isEmpty()) {
                    UserVO vo = Result2Vo.getUserVO(r);
                    result.put(vo.getId(), vo);
                }
            }
        }
        return result;
    }

    public static List<WorkerQueryInfoVO> getUserVOListFillList(List<WorkerQueryInfoVO> workersList) {
        Map<String, WorkerQueryInfoVO> userIdMap = workersList.stream().collect(Collectors.toMap(WorkerQueryInfoVO::getUserId, bs -> {
            return bs;
        }));

        Result[] rs = HBaseUtil.getRows("v2_rp_tb_user4user", Arrays.asList(userIdMap.keySet().toArray()));
        for (Result r : rs) {
            if (null != r && !r.isEmpty()) {
                String userId = Bytes.toString(r.getRow());
                WorkerQueryInfoVO infoVO = userIdMap.get(userId);
//                UserVO vo = Result2Vo.getUserVO(r);
                infoVO.setAvator(Result2Vo.stringCol(r, "avator"));
                infoVO.setMsisdn(Result2Vo.stringCol(r, "msisdn"));
                userIdMap.put(userId, infoVO);
            }
        }
        WorkerQueryInfoVO[] vos = new WorkerQueryInfoVO[userIdMap.values().size()];
        return Arrays.asList(userIdMap.values().toArray(vos));

    }


    public static List<WorkerQueryInfoVO> getWorkerLocationList(List<WorkerQueryInfoVO> workersList) {
        Map<String, WorkerQueryInfoVO> workerVOMap = workersList.stream().collect(Collectors.toMap(WorkerQueryInfoVO::getId, bs -> {
            return bs;
        }));

        Result[] rs = HBaseUtil.getRows("v2_rp_tb_worker_location", Arrays.asList(workerVOMap.keySet().toArray()));
        for (Result r : rs) {
            if (null != r && !r.isEmpty()) {
                String workerId = Bytes.toString(r.getRow());
                Result2Vo.getWorkerInfoLocation(r, workerVOMap.get(workerId));
            }
        }

        WorkerQueryInfoVO[] vos = new WorkerQueryInfoVO[workerVOMap.values().size()];
        return Arrays.asList(workerVOMap.values().toArray(vos));

    }

    public static List<WorkerServiceVO> getWorkerServiceList(List<WorkerServiceVO> serviceVOList) {
        Map<String, List<WorkerServiceVO>> serviceIdMap = serviceVOList.stream().collect(Collectors.groupingBy(WorkerServiceVO::getServiceId));
        List<WorkerServiceVO> temp = new ArrayList<>(200);
        Result[] rs = HBaseUtil.getRows("v2_rp_tb_service", Arrays.asList(serviceIdMap.keySet().toArray()));
        for (Result r : rs) {
            if (null != r && !r.isEmpty()) {
                String serviceId = Bytes.toString(r.getRow());
                List<WorkerServiceVO> voList = serviceIdMap.get(serviceId);
                ServiceVO serviceVo = Result2Vo.getServiceVOFromR(r);
                for (WorkerServiceVO vo : voList) {
                    vo.setServiceName(serviceVo.getName());
                    vo.setServiceMemo(serviceVo.getMemo());
                    vo.setPeople(serviceVo.getPeople());
                    vo.setType(serviceVo.getType());
                    temp.add(vo);
                }
            }
        }

//        return Arrays.asList((WorkerServiceVO) serviceIdMap.values());
        return temp;

    }

    public static List<WorkerQueryInfoVO> getWorkerVoList(List<WorkerQueryInfoVO> workerVOList) {
        Map<String, WorkerQueryInfoVO> workerVoMap = workerVOList.stream().collect(Collectors.toMap(WorkerQueryInfoVO::getId, bs -> {
            return bs;
        }));

        Result[] rs = HBaseUtil.getRows("v2_rp_tb_worker", Arrays.asList(workerVoMap.keySet().toArray()));
        for (Result r : rs) {
            if (null != r && !r.isEmpty()) {
                String serviceId = Bytes.toString(r.getRow());
                Result2Vo.fillWorkerVO(r, workerVoMap.get(serviceId));
            }
        }
        WorkerQueryInfoVO[] vos = new WorkerQueryInfoVO[workerVoMap.values().size()];
        return Arrays.asList(workerVoMap.values().toArray(vos));

    }


    public static void getWorkerLocation(WorkerVO workerVO) {
        Result r = HBaseUtil.getRow("v2_rp_tb_worker_location", Bytes.toBytes(workerVO.getId()));
        if (null != r && !r.isEmpty()) {
            Result2Vo.getWorkerLocation(r, workerVO);
        }
    }


    /**
     * 根据worker_ids  获取职业人服务
     *
     * @param ids
     * @return
     */
    public static Map<String, List<WorkerServiceVO>> getWorkerServices(List<String> ids) {
        Map<String, List<WorkerServiceVO>> result = new HashMap<>();
        for (String id : ids) {
            ResultScanner rs = HBaseUtil.queryRowsWithKeys("v2_rp_tb_worker_service", id + "=0=", id + "=0>");
            Iterator<Result> it = rs.iterator();
            List<WorkerServiceVO> list = new ArrayList<>();
            while (it.hasNext()) {
                list.addAll(Result2Vo.getWorkerServiceVO(it.next()));
            }
            result.put(id, list);
        }
        return result;
    }

    public static Map<String, List<WorkerServiceVO>> getWorkerServicesList(List<String> ids) {
        Map<String, List<WorkerServiceVO>> result = new HashMap<>();
        FilterList filter = HBaseUtil.getFilterList("0", null, null);
        ResultScanner rs = HBaseUtil.get("v2_rp_tb_worker_service", filter);
        Iterator<Result> it = rs.iterator();
        while (it.hasNext()) {
            List<WorkerServiceVO> list = Result2Vo.getWorkerServiceVO(it.next());
            String workerId = list.get(0).getWorkerId();
            if (result.containsKey(workerId)) {
                List<WorkerServiceVO> temp = result.get(workerId);
                temp.addAll(list);
                result.put(workerId, temp);
            } else {
                result.put(workerId, list);
            }
        }
        return result;
    }

    /**
     * 查询婚礼订单信息
     *
     * @param ids
     * @param yearMonth
     * @param endYearMonth
     * @param dateType
     * @return
     */
    public static Map<String, List<WorkerWeddingOrderVO>> getWorkerWeddingOrders(List<String> ids, String
            yearMonth,
                                                                                 String endYearMonth, String dateType) {
        Map<String, List<WorkerWeddingOrderVO>> result = new HashMap<>();
        for (String id : ids) {
            ResultScanner rs = HBaseUtil.queryRowsWithKeys("v2_rp_tb_worker_wedding_order",
                    id + "=" + dateType + "=" + yearMonth, id + "=" + dateType + "=" + endYearMonth);
            Iterator<Result> it = rs.iterator();
            List<WorkerWeddingOrderVO> list = new ArrayList<>();
            while (it.hasNext()) {
                WorkerWeddingOrderVO vo = Result2Vo.getWorkerWeddingOrderVO(it.next());
                if (!"2".equals(vo.getWeddingOrderStatus()) && "1".equals(vo.getIsUse()) && !"1".equals(vo.getFlag())) {
                    list.add(vo);
                }
            }
            result.put(id, list);
        }
        return result;
    }


    /**
     * 查询婚礼组合成员信息
     *
     * @param ids
     * @param yearMonth
     * @param endYearMonth
     * @return
     */
    public static Map<String, List<UserWeddingTeamMemberVO>> getUserWeddingTeamMembers(List<String> ids,
                                                                                       String yearMonth, String endYearMonth) {
        Map<String, List<UserWeddingTeamMemberVO>> result = new HashMap<>();
        for (String id : ids) {
            ResultScanner rs = HBaseUtil.queryRowsWithKeys("v2_rp_tb_user_wedding_team_member",
                    id + "=" + yearMonth, id + "=" + endYearMonth);
            Iterator<Result> it = rs.iterator();
            List<UserWeddingTeamMemberVO> list = new ArrayList<>();
            while (it.hasNext()) {
                list.add(Result2Vo.getUserWeddingTeamMemberVO(it.next()));
            }
            result.put(id, list);
        }
        return result;
    }

    /**
     * 根据ids查询用户信息
     *
     * @param ids
     * @return
     */
    public static Map<String, WorkerMonthOrderLimitVO> getWorkerMonthOrderLimits(List<String> ids, String
            year, String month) {
        Map<String, WorkerMonthOrderLimitVO> result = new HashMap<>();
        for (String id : ids) {
            Result r = HBaseUtil.getRow("v2_rp_tb_worker_month_order_limit", Bytes.toBytes(id + "=" + year + "=" + Integer.valueOf(month)));
            if (null != r && !r.isEmpty()) {
                result.put(id, Result2Vo.getWorkerMonthOrderLimitVO(r));
            }
        }
        return result;
    }


    public static Map<String, WorkerMonthOrderLimitVO> getWorkerMonthOrderLimitsList(List<String> ids, String
            year, String month) {
        Map<String, WorkerMonthOrderLimitVO> result = new HashMap<>();
        int size = ids.size();
        List<List> list = new ArrayList<>();
        List<String> temp = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            temp.add(ids.get(i) + "=" + year + "=" + Integer.valueOf(month));
            if (i % 100 == 0 || i == size - 1) {
                list.add(temp);
                if (i != size - 1) temp = new ArrayList<>();
            }
        }

        for (List l : list) {
            Result[] rs = HBaseUtil.getRows("v2_rp_tb_worker_month_order_limit", l);
            for (Result r : rs) {
                if (null != r && !r.isEmpty()) {
                    WorkerMonthOrderLimitVO vo = Result2Vo.getWorkerMonthOrderLimitVO(r);
                    result.put(vo.getWorkerId(), vo);
                }
            }
        }

        return result;
    }
}
